package bid.yanhj.autocompletetextview.autocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private AutoCompleteTextView mAutoCompleteTextView;
    private MultiAutoCompleteTextView mMultiAutoCompleteTextView;

    private String[] res = {"beijing","beijing1","beijing2","beijing3","beijing4"};


    //
    private ToggleButton mToggleButton;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         *  初始化组件
         *  需要适配器
         *  初始化数据源
         *  adapter 和autoCompleteTextView绑定
         */
        mAutoCompleteTextView = findViewById(R.id.autoCompleteTextView);

        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res);

        mAutoCompleteTextView.setAdapter(adapter);

        // MultiAutoCompleteTextView
        // 设置分隔符
        mMultiAutoCompleteTextView = findViewById(R.id.multiAutoCompleteTextView);

        mMultiAutoCompleteTextView.setAdapter(adapter);
        mMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());


        mToggleButton = findViewById(R.id.toggleButton);
        mImageView = findViewById(R.id.imageView);

        /**
         * 设置监听器
         */
        mToggleButton.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mImageView.setBackgroundResource(isChecked?R.mipmap.ic_launcher:R.mipmap.ic_launcher_round);
    }
}
